using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MyAppBox;

namespace SysFrame.BP
{
    /// <summary>
    /// 基础造作
    /// </summary>
    public class DataBase
    {
        /// <summary>
        /// 得到表最大ID
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="column">列名</param>
        /// <returns></returns>
        public static int GetMaxId(string tablename)
        {

            List<string> Resultlist = new List<string>();
            StringBuilder sb = new StringBuilder();
            List<string> listcmd = new List<string>();
            sb.Append(" select count(*)+1 from " + tablename + "");
            listcmd.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                //IniOP iniop = new IniOP();
                string sqltype = SQLTYPE;
                //string sqltype = "2";
                if (sqltype == "1")
                {
                    //using (IDataReader dr = DataAccessor.ExecuteReader(listcmd[0].ToString()))
                    //{
                    //    while (dr.Read())
                    //    {
                    //        try
                    //        {
                    //            return Convert.ToInt32(dr[0].ToString());
                    //        }
                    //     catch { return 1; }

                    //    }
                    //}
                }
                else if (sqltype == "2")
                {
                     using (IDataReader dr = DbHelperMySQL.ExecuteReader(listcmd[0].ToString()))
                    {
                        while (dr.Read())
                        {
                            try
                            {
                                return Convert.ToInt32(dr[0].ToString());
                            }
                            catch { return 1; }

                        }
                    }
                }
            }
            catch { return 1; }
            return 1;

        }

        /// <summary>
        /// 得到表最大ID
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="column">列名</param>
        /// <returns></returns>
        public static int GetMaxId(string tablename, string column)
        {
            List<string> Resultlist = new List<string>();
            StringBuilder sb = new StringBuilder();
            List<string> listcmd = new List<string>();
            sb.Append(" select max(" + column + ")+1 from " + tablename + "");
            listcmd.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                //IniOP iniop = new IniOP();
                string sqltype = SQLTYPE;
                //string sqltype = "2";
                if (sqltype == "1")
                {
                    //using (IDataReader dr = DataAccessor.ExecuteReader(listcmd[0].ToString()))
                    //{
                    //    while (dr.Read())
                    //    {
                    //        try
                    //        {
                    //            return Convert.ToInt32(dr[0].ToString());
                    //        }
                    //        catch { return 1; }

                    //    }
                    //}
                }
                else if (sqltype == "2")
                {
                     using (IDataReader dr = DbHelperMySQL.ExecuteReader(listcmd[0].ToString()))
                    {
                        while (dr.Read())
                        {
                            try
                            {
                                return Convert.ToInt32(dr[0].ToString());
                            }
                            catch { return 1; }

                        }
                    }
                }
            }
            catch { return 1; }
            return 1;
        }

        /// <summary>
        /// 得到表最大ID
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="column">列名</param>
        /// <returns></returns>
        public static int GetMaxId(string tablename, string column, string where)
        {
            List<string> Resultlist = new List<string>();
            StringBuilder sb = new StringBuilder();
            List<string> listcmd = new List<string>();
            sb.Append(" select max(" + column + ")+1 from " + tablename + "");
            if (where != "")
            {
                sb.Append("  where " + where + "");
            }
            listcmd.Add(sb.ToString());
            sb.Remove(0, sb.ToString().Length);
            try
            {
                //IniOP iniop = new IniOP();
                string sqltype = SQLTYPE;
                //string sqltype = "2";
                if (sqltype == "1")
                {
                    //using (IDataReader dr = DataAccessor.ExecuteReader(listcmd[0].ToString()))
                    //{
                    //    while (dr.Read())
                    //    {
                    //        try
                    //        {
                    //            return Convert.ToInt32(dr[0].ToString());
                    //        }
                    //     catch { return 1; }

                    //    }
                    //}
                }
                else if (sqltype == "2")
                {
                     using (IDataReader dr = DbHelperMySQL.ExecuteReader(listcmd[0].ToString()))
                    {
                        while (dr.Read())
                        {
                            try
                            {
                                return Convert.ToInt32(dr[0].ToString());
                            }
                            catch { return 1; }

                        }
                    }
                }
            }
            catch { return 1; }
            return 1;

        }
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static string SQLTYPE { get; set; }


    }
}
